package com.example.administrator.acmapp.Fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoyz.widget.PullRefreshLayout;
import com.example.administrator.acmapp.Activity.MainActivity;
import com.example.administrator.acmapp.Assist.CircleImageView;
import com.example.administrator.acmapp.Assist.NoScrollViewPager;
import com.example.administrator.acmapp.Fragment.CommunityFragmentPage.AcmNewsFragment;
import com.example.administrator.acmapp.Fragment.CommunityFragmentPage.NewItFragment;
import com.example.administrator.acmapp.Fragment.CommunityFragmentPage.NewOosFragment;
import com.example.administrator.acmapp.Fragment.CommunityFragmentPage.NewVideoFragment;
import com.example.administrator.acmapp.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.oragee.banners.BannerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/2/1/001.
 */

public class CommunityFragment extends Fragment {


    private View view;
    private List<View> mListImage;
    private List<Fragment> fragmentList;
    private int[] imgs = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
    private NewVideoFragment newVideoFragment;
    private NewOosFragment newOosFragment;
    private NewItFragment newItFragment;
    private NoScrollViewPager mContentViewPage;
    private AcmNewsFragment acmNewsFragment;
    private TextView mAcmNewsTv;
    private TextView mNewItTv;
    private TextView mNewOosTv;
    private TextView mNewVideo;
    CircleImageView circleImageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.community_fragment_layout, container, false);
        mListImage = new ArrayList<>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getContext());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            mListImage.add(image);
        }
        BannerView bannerView = view.findViewById(R.id.community_fragmnet_bannerview);
        bannerView.startLoop(true);
        bannerView.setViewList(mListImage);
        final PullRefreshLayout layout = (PullRefreshLayout) view.findViewById(R.id.question_fragment_pullRefreshLayout);

        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // start refresh
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                    }
                }, 3000);
            }
        });
        layout.setColor(Color.parseColor("#7fd8bf"));
        layout.setRefreshStyle(PullRefreshLayout.STYLE_SMARTISAN);
        layout.setRefreshing(false);
        initView();
        return view;
    }

    public CommunityFragment() {

    }

    private void initView(){
        final MainActivity mainActivity = (MainActivity) getActivity();


        mContentViewPage = view.findViewById(R.id.community_fragmnet_news_viewPage);
        mContentViewPage.setScanScroll(false);
        mAcmNewsTv = view.findViewById(R.id.acm_news_fragment_tv);
        mNewItTv = view.findViewById(R.id.new_it_fragment_tv);
        mNewOosTv = view.findViewById(R.id.new_oos_fragment_tv);
        mNewVideo = view.findViewById(R.id.new_video_fragment_tv);
        circleImageView = view.findViewById(R.id.one_circleimageview_click);
        circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainActivity.menu.toggle();
            }
        });
        fragmentList = new ArrayList<>();
        acmNewsFragment = new AcmNewsFragment();
        newItFragment = new NewItFragment();
        newOosFragment = new NewOosFragment();
        newVideoFragment = new NewVideoFragment();
        fragmentList.add(acmNewsFragment);
        fragmentList.add(newItFragment);
        fragmentList.add(newOosFragment);
        fragmentList.add(newVideoFragment);
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        mContentViewPage.setAdapter(fragmentPagerAdapter);
        mAcmNewsTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContentViewPage.setCurrentItem(0);
            }
        });
        mNewItTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContentViewPage.setCurrentItem(1);
            }
        });
        mNewOosTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContentViewPage.setCurrentItem(2);
            }
        });
        mNewVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContentViewPage.setCurrentItem(3);
            }
        });
        mContentViewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        mAcmNewsTv.setBackgroundResource(R.drawable.nav_line_select);
                        mNewItTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewOosTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewVideo.setBackgroundResource(R.drawable.nav_line_unselect);
                        mAcmNewsTv.setTextColor(Color.parseColor("#7fd8bf"));
                        mNewItTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewOosTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewVideo.setTextColor(Color.parseColor("#8C8C8C"));
                        break;
                    case 1:
                        mAcmNewsTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewItTv.setBackgroundResource(R.drawable.nav_line_select);
                        mNewOosTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewVideo.setBackgroundResource(R.drawable.nav_line_unselect);
                        mAcmNewsTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewItTv.setTextColor(Color.parseColor("#7fd8bf"));
                        mNewOosTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewVideo.setTextColor(Color.parseColor("#8C8C8C"));
                        break;
                    case 2:
                        mAcmNewsTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewItTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewOosTv.setBackgroundResource(R.drawable.nav_line_select);
                        mNewVideo.setBackgroundResource(R.drawable.nav_line_unselect);
                        mAcmNewsTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewItTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewOosTv.setTextColor(Color.parseColor("#7fd8bf"));
                        mNewVideo.setTextColor(Color.parseColor("#8C8C8C"));
                        break;
                    case 3:
                        mAcmNewsTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewItTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewOosTv.setBackgroundResource(R.drawable.nav_line_unselect);
                        mNewVideo.setBackgroundResource(R.drawable.nav_line_select);
                        mAcmNewsTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewItTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewOosTv.setTextColor(Color.parseColor("#8C8C8C"));
                        mNewVideo.setTextColor(Color.parseColor("#7fd8bf"));
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
